package cn.wolfcode.car.business.web.controller.carPackageAudit;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.query.CarPackageAuditQuery;
import cn.wolfcode.car.business.query.CarPackageAuditQueryUp;
import cn.wolfcode.car.business.service.CarPackageAuditService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.StringUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.commons.io.IOUtils;
import org.apache.poi.util.Removal;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author WaveHi
 * @Date 17/11/2021 下午 3:00
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController {

    @Autowired
    private CarPackageAuditService carPackageAuditService;

    private final String prefix = "/business/carPackageAudit/";

    @RequiresPermissions("business:carPackageAudit:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:carPackageAudit:view")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> query(
            @RequestParam(value = "pageNo",required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            CarPackageAuditQuery carPackageAuditQuery) {
        //Judge data
        pageNo = pageNo != null ? pageNo : 1;
        pageSize = pageSize != null ? pageSize : 3;
        //Correct data
        //Query
        return carPackageAuditService.getPartOfCarPackageAudits(carPackageAuditQuery);
    }

    @RequestMapping("/processImg")
    public void  processImg(
            @RequestParam(value = "id", required = false) Long id,
            HttpServletResponse response) {
        if(id != null) {
            InputStream inputStream=carPackageAuditService.getProcessImageByAuditId(id);
            try {
                IOUtils.copy(inputStream,response.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(
            @RequestParam(value = "id", required = false) Long id){
        if(id != null) {
            carPackageAuditService.cancel(id);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequestMapping("/todoPage")
    public String todoPage() {
        return prefix + "todoPage";
    }

    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> todoQuery() {
        User user = ShiroUtils.getUser();
        CarPackageAuditQueryUp carPackageAuditQuery = new CarPackageAuditQueryUp();
        carPackageAuditQuery.setAuditorId(user.getId());
        carPackageAuditQuery.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        return carPackageAuditService.getWork(carPackageAuditQuery);
    }

    @RequestMapping("/auditPage")
    public String auditPage(
            @RequestParam(value = "id", required = false) Long id,
            Model model) {
        model.addAttribute("id", id);
        return prefix + "auditPage";
    }

    @RequestMapping("/audit")
    @ResponseBody
    public AjaxResult audit(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
            @RequestParam(value = "info", required = false) String info) {
        //Judge data
        boolean empty = StringUtils.isEmpty(info);
        //Correct data
        if(!empty) {
            info = info.trim();
        }
        //Audit
        if(id != null && auditStatus != null) {
            carPackageAuditService.audit(id, auditStatus, info);
            return AjaxResult.success();
        }
        throw new BusinessException("缺失审批条件！");
    }

    @RequestMapping("/donePage")
    public String donePage() {
        return prefix + "donePage";
    }

    @RequiresPermissions("business:carPackageAudit:doneQuery")
    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> doneQuery(){
        User user = ShiroUtils.getUser();
        return carPackageAuditService.getWork(user.getUserName());
    }

}
